#include "TrueText/Config.h"
#include "Spans.h"

namespace TrueText {

void IncludePointInRect(Rectangle& rect, int x, int y)
{
	rect.left = std::min(x, rect.left);
	rect.top = std::min(y, rect.top);
	rect.right = std::max(x, rect.right);
	rect.bottom = std::max(y, rect.bottom);
}

Rectangle IncludeSpans(const Spans& spans, const Rectangle& rect)
{
	Rectangle result = rect;

	if (spans.empty())
		return rect;

	if (result.left == 0)
		result.left = spans.front().x;
	if (result.top == 0)
		result.top = spans.front().y;
	if (result.right == 0)
		result.right = spans.front().x;
	if (result.bottom == 0)
		result.bottom = spans.front().y;

	for (Spans::const_iterator s = spans.begin(); s != spans.end(); ++s)
	{
		IncludePointInRect(result, s->x, s->y);
		IncludePointInRect(result, s->x + s->width - 1, s->y);
	}

	return result;
}

}